﻿using Lib.WPF.Core;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace Lib.WPF.Controls; 
/// <summary>
/// SoftKeyBoard.xaml 的交互逻辑
/// </summary>
public partial class SoftKeyBoard : UserControl {
    /// <summary>
    /// 构造函数
    /// </summary>
    public SoftKeyBoard() {
        InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    private bool _isCapsLock = false;
    /// <summary>
    /// 
    /// </summary>
    private bool _isShift = false;
    /// <summary>
    /// 普通键点击事件
    /// </summary>
    private void KeyButton_Click(object Sender, RoutedEventArgs e) {
        if (Sender is Button Button) {
            var Key = Button.Content.ToString();
            if (_isShift || _isCapsLock) {
                Key = Key.ToUpper();
            } else {
                Key = Key.ToLower();
            }
            SendKeyToFocusedControl(Key);
            if (_isShift) {
                _isShift = false;
                UPDateKeyboardState();
            }
        }
    }
    /// <summary>
    /// 空格键
    /// </summary>
    private void Space_Click(object Sender, RoutedEventArgs e) {
        SendKeyToFocusedControl(" ");
    }
    /// <summary>
    /// 退格键
    /// </summary>
    private void Backspace_Click(object Sender, RoutedEventArgs e) {
        if (TargetElement != null) {
            if (IsPasswordBox) {
                if (!string.IsNullOrEmpty(this.PasswordBox.Password)) {
                    this.PasswordBox.Password = this.PasswordBox.Password[..^1];
                }
            } else {
                if (!string.IsNullOrEmpty(this.TextBox.Text)) {
                    this.TextBox.Text = this.TextBox.Text[..^1];
                    this.TextBox.CaretIndex = this.TextBox.Text.Length;
                }
            }
        }
    }
    /// <summary>
    /// 回车键
    /// </summary>
    private void Enter_Click(object Sender, RoutedEventArgs e) {
        /**/
        if (this.Enter != null) {
            if (IsPasswordBox) {
                this.Enter.Execute(this.PasswordBox);
            } else {
                this.Enter.Execute(this.TextBox);
            }
        }
    }
    /// <summary>
    /// SHIFT键
    /// </summary>
    private void Shift_Click(object Sender, RoutedEventArgs e) {
        _isShift = !_isShift;
        UPDateKeyboardState();
    }
    /// <summary>
    /// CapsLock键
    /// </summary>
    private void CapsLock_Click(object Sender, RoutedEventArgs e) {
        _isCapsLock = !_isCapsLock;
        UPDateKeyboardState();
    }
    /// <summary>
    /// 更新键盘状态（大小写）
    /// </summary>
    private void UPDateKeyboardState() {
        foreach (var Child in this.FindVisualChildren<Button>()) {
            if (Child.Content != null && Child.Content.ToString().Length == 1 &&
                char.IsLetter(Child.Content.ToString()[0])) {
                if (_isShift || _isCapsLock) {
                    Child.Content = Child.Content.ToString().ToUpper();
                } else {
                    Child.Content = Child.Content.ToString().ToLower();
                }
            }
        }
    }
    /// <summary>
    /// 发送按键到焦点控件
    /// </summary>
    /// <param name="Key"></param>
    private void SendKeyToFocusedControl(string Key) {
        if (TargetElement != null) {
            if (IsPasswordBox) {
                this.PasswordBox.Password += Key;
            } else {
                int CaretIndex = this.TextBox.CaretIndex;
                this.TextBox.Text = this.TextBox.Text.Insert(CaretIndex, Key);
                this.TextBox.CaretIndex = CaretIndex + 1;
            }
        }
    }
    /// <summary>
    /// 
    /// </summary>
    private ICommand Enter;
    /// <summary>
    /// 
    /// </summary>
    private bool IsPasswordBox;
    /// <summary>
    /// 
    /// </summary>
    private PasswordBox PasswordBox;
    /// <summary>
    /// 
    /// </summary>
    private TextBox TextBox;
    /// <summary>
    /// 
    /// </summary>
    public static readonly DependencyProperty TargetElementProperty = DependencyProperty.Register("TargetElement", typeof(UIElement), typeof(SoftKeyBoard), new PropertyMetadata(null));
    /// <summary>
    /// 
    /// </summary>
    public UIElement TargetElement {
        get => (UIElement)GetValue(TargetElementProperty);
        set => SetValue(TargetElementProperty, value);
    }
    /// <summary>
    /// 
    /// </summary>
    public void Set(UIElement PlacementTarget, ICommand CMD) {
        if (CMD != null) {
            this.Enter = CMD;
        }
        if(this.TargetElement == null) {
            if (PlacementTarget is Button Button) {
                this.PasswordBox = Button.FindSiblings<PasswordBox>();
                this.IsPasswordBox = true;
            } else if (PlacementTarget is TextBox TextBox) {
                this.TextBox = TextBox;
            } else if (PlacementTarget is PasswordBox PasswordBox) {
                this.PasswordBox = PasswordBox;
                this.IsPasswordBox = true;
            }
            this.TargetElement = PlacementTarget;
        }
    }
}
